import VXETable from 'vxe-table'

import VxeSelectCell from '@/components/zz/VxeTable/components/cells/VxeSelectCell'
// import VxeNormalCell from '@/components/zz/VxeTable/components/cells/VxeNormalCell'
import VxeDatePickerCell from '@/components/zz/VxeTable/components/cells/VxeDatePickerCell'
import VxeDictCell from '@/components/zz/VxeTable/components/cells/VxeDictCell'

import VxeToolbarAdd from '@/components/zz/VxeTable/components/toolbar/VxeToolbarAdd'
import VxeToolbarButton from '@/components/zz/VxeTable/components/toolbar/VxeToolbarButton'
import VxeToolbarTool from '@/components/zz/VxeTable/components/toolbar/VxeToolbarTool'
import VxeToolbarBatchDel from '@/components/zz/VxeTable/components/toolbar/VxeToolbarBatchDel'
import VxeToolbarDel from '@/components/zz/VxeTable/components/toolbar/VxeToolbarDel'
import VxeToolbarExport from '@/components/zz/VxeTable/components/toolbar/VxeToolbarExport'
import VxeToolbarSearch from '@/components/zz/VxeTable/components/toolbar/VxeToolbarSearch'

VXETable.formats.add('dictText', ({ column, row, cellValue }) => { // 字典格式化显示
  return (row['textMap'] && row['textMap'][column.field + '$DICT_TEXT_']) || cellValue
})

/** 注册可编辑组件 */
export function installEditRender(type, comp) {
  // 注册自定义组件
  VXETable.renderer.add(type, {
    renderEdit: createEditRender(comp),
    renderCell: (comp.enhanced && comp.enhanced.translate) ? createEditRender(comp, 'renderCell') : undefined
  })
}

/** 注册单元格渲染组件 */
export function installRenderDefault(type, comp) {
  // 注册自定义组件
  VXETable.renderer.add(type, {
    renderDefault: createEditRender(comp)
  })
}

/** 注册toolbar 左侧组件 */
function installToolbarButtonRender(comp) {
  // 注册自定义组件
  VXETable.renderer.add(comp.name, {
    renderToolbarButton(h, renderOptions, params) {
      return [
        h(comp, {
          props: {
            renderOptions: renderOptions,
            params: params,
            toolbarProps: { ...renderOptions.props }
          }
        })
      ]
    }
  })
}

/** 注册toolbar 右侧组件 */
function installToolbarToolRender(comp) {
  // 注册自定义组件
  VXETable.renderer.add(comp.name, {
    renderToolbarTool(h, renderOptions, params) {
      return [
        h(comp, {
          props: {
            renderOptions: renderOptions,
            params: params,
            toolbarProps: { ...renderOptions.props }
          }
        })
      ]
    }
  })
}

function createEditRender(comp, renderType) {
  return function(h, renderOptions, params) {
    const { attr, props } = renderOptions
    const { row, column } = params
    return [h(comp, {
      attr,
      props: Object.assign({}, { cellProps: props }, { value: row[column.property], renderOptions: renderOptions, params: params, renderType: renderType })
    })]
  }
}

// 注册自定义渲染组件
installEditRender('ZDictSelectTag', VxeSelectCell)
installEditRender('ZDatePicker', VxeDatePickerCell)

// installRenderDefault('ZNormalCell', VxeNormalCell)
installRenderDefault('ZDictCell', VxeDictCell)

// 注册toolbar组件
installToolbarButtonRender(VxeToolbarAdd)
installToolbarButtonRender(VxeToolbarBatchDel)
installToolbarButtonRender(VxeToolbarDel)
installToolbarButtonRender(VxeToolbarButton)
installToolbarToolRender(VxeToolbarExport)
installToolbarToolRender(VxeToolbarSearch) // 高级搜索
installToolbarToolRender(VxeToolbarTool)
